package com.ktjy.ktmall.servlet;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.ktjy.ktmall.entity.AjaxResult;
import com.ktjy.ktmall.entity.Role;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.Collectors;

@WebServlet(name = "TestServlet", urlPatterns = "/uni/manager/test")
public class TestServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        System.out.println(name);
        //以流的形式接收请求体的数据
        String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        System.out.println("params:" + params);
        JSONObject entries = JSONUtil.parseObj(params);
        String opr = entries.get("opr", String.class);
        System.out.println("opr:" + opr);
        Role role = entries.get("role", Role.class);
        System.out.println("role:" + role);
        AjaxResult result = AjaxResult.success("成功!", name);
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
        out.flush();
        out.close();
    }
}
